package com.tuanbiao.ui.auth;

import android.content.Context;
import android.content.Intent;
import android.text.InputType;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.abiao.lib_utils.StringUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.AuthController;
import com.tuanbiao.databinding.ActivityAuthLocalBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.InputDialog;
import com.tuanbiao.models.AuthModel;
import com.tuanbiao.presenter.auth.AuthLocalPresenter;
import com.tuanbiao.ui.auth.adapter.AuthLocalTxtAdapter;
import com.tuanbiao.utils.ClickUtil;
import com.tuanbiao.widget.RegexEditText;

import java.io.File;
import java.util.List;

public class AuthLocalActivity extends BasePresenterActivity<AuthLocalPresenter, ActivityAuthLocalBinding> implements AuthController.LocalView {

    private AuthLocalTxtAdapter authLocalTxtAdapter;

    public static void start(Context context) {
        Intent starter = new Intent(context, AuthLocalActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivityAuthLocalBinding getBindingView() {
        return ActivityAuthLocalBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.rvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        authLocalTxtAdapter = new AuthLocalTxtAdapter();
        authLocalTxtAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            File file = (File) adapter.getItem(position);
            showTxtDialog(file);
        });
        binding.rvContent.setAdapter(authLocalTxtAdapter);
        binding.srlContent.setEnableLoadMore(false);
        binding.srlContent.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.refresh();
            }
        });
        binding.srlContent.autoRefresh();
    }

    private void showTxtDialog(File file) {
        new InputDialog.Builder(getActivity())
                .setTitle("提示")
                .setHint("请输入六位提取码")
                .setAutoDismiss(false)
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .setInputRegex(RegexEditText.REGEX_NUMBER)
                .setMaxLength(6)
                .setListener(new InputDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog, String content) {
                        if (StringUtils.length(content) != 6) {
                            showMessage("请输入六位提取码");
                            return;
                        }
                        mPresenter.getAuthFile(file, content);
                        dialog.dismiss();
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();
    }


    @Override
    protected AuthLocalPresenter createPresenter() {
        return new AuthLocalPresenter(new AuthModel(), this);
    }

    @Override
    public void setItems(List<File> files) {
        binding.srlContent.finishRefresh();
        authLocalTxtAdapter.setNewInstance(files);
    }
}